import yaml
from tencentcloud.common import credential
from tencentcloud.cdn.v20180606 import cdn_client
from tencentcloud.cdn.v20180606 import models


class TencentApiTools:

    def describe_cdn_ip(self, ips):
        with open("/tmp/tencentapi.yaml") as yaml_file:
            yam_obj = yaml.safe_load(yaml_file)
            if "SecretId" in yam_obj:
                secret_id = yam_obj["SecretId"]
            else:
                raise RuntimeError("SecretId is not found")
            if "SecretKey" in yam_obj:
                secret_key = yam_obj["SecretKey"]
            else:
                raise RuntimeError("SecretKey is not found")

            # print(secret_id, secret_key)

            client_credential = credential.Credential(secret_id, secret_key)
            client = cdn_client.CdnClient(client_credential, "ap-beijing")
            # ips = ["123.150.72.123", "222.124.145.242"]
            describe_cdn_ip_request = models.DescribeCdnIpRequest()
            describe_cdn_ip_request.Ips = ips
            response = client.DescribeCdnIp(describe_cdn_ip_request)
            # print(response)
            cdn_ips = []
            if len(response.Ips) > 0:
                for ip in response.Ips:
                    if ip.Platform == 'yes':
                        cdn_ips.append(ip)
            else:
                raise RuntimeError("response no IPs, post ips: ", ips)

            print(cdn_ips)


if __name__ == '__main__':
    TencentApiTools().describe_cdn_ip()
